//
//  GLBaseAnimatedTransition.m
//  HGLAnimation
//
//  Created by huang long on 17/1/18.
//  Copyright © 2017年 huang gu long. All rights reserved.
//

#import "GLBaseAnimatedTransition.h"

@implementation GLBaseAnimatedTransition

+ (instancetype)transitionWithTransitionType:(GLAnimatedTransitionType)transitionType{
    return [[self alloc] initWithTransitionType:transitionType];
}

- (instancetype)initWithTransitionType:(GLAnimatedTransitionType)transitionType{
    self = [self init];
    if (self) {
        _transitionType = transitionType;
    }
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _duration = 0.25;
    }
    return self;
}

// 动画运行时间
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{
    return self.duration;
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
    if (self.transitionType == GLAnimatedTransitionTypePresent) {
        [self presentAnimateTransition:transitionContext];
    }else if (self.transitionType == GLAnimatedTransitionTypeDismiss){
        [self dismissAnimateTransition:transitionContext];
    }else if (self.transitionType == GLAnimatedTransitionTypePush){
        [self pushAnimateTransition:transitionContext];
    }else{
        [self popAnimateTransition:transitionContext];
    }
}


#pragma mark - subclass override
- (void)presentAnimateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
    //动画 效果
    
    UIView *containerView = [transitionContext containerView];//容器
    UIViewController *toVC   = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *toView = [toVC view];
    CGRect rect =  [transitionContext finalFrameForViewController:toVC];
    [containerView addSubview:toView];
    //    rect = CGRectInset(rect, 50, 50);
    toView.frame = CGRectOffset(rect, 0, [UIScreen mainScreen].bounds.size.height);
    [UIView animateWithDuration:self.duration animations:^{
        toView.frame = rect;
    }completion:^(BOOL finished) {
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
}


- (void)dismissAnimateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
    
}


- (void)pushAnimateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
    
}

- (void)popAnimateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
    
}

@end
